package com.czy.workflow.core.selector;

import com.czy.workflow.core.context.FlowContext;
import com.czy.workflow.core.exception.FlowExpressionException;
import com.czy.workflow.core.model.FlowBranch;
import com.czy.workflow.core.model.FlowNode;
import com.czy.workflow.core.model.TaskInfo;
import com.czy.workflow.core.util.ExpressionUtil;

import java.util.ArrayList;
import java.util.List;

public class AutoNodeBranchSelector extends AbstractNodeBranchSelector{

    @Override
    public List<FlowNode> selectNextNode(FlowContext flowContext, TaskInfo taskInfo) throws FlowExpressionException {
        List<FlowBranch> validBranches = new ArrayList<>();
        List<FlowBranch> branches = flowContext.getFlowService().getNodeBranches(taskInfo.getFlowId(), taskInfo.getNode().getId());

        for(FlowBranch branch : branches){
            if (ExpressionUtil.exeExpression(branch.getExpression(), taskInfo)){
                validBranches.add(branch);
            }
        }
        return getNodesByBranch(flowContext, validBranches);
    }
}
